package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.example.erp.domain.*;
import org.example.erp.service.WarehouseAreaService;
import org.example.erp.service.WarehouseLocationService;
import org.example.erp.service.WarehouseService;
import org.example.erp.service.WmItemRecptLineService;
import org.example.erp.service.impl.WarehouseLocationServiceImpl;
import org.example.erp.service.impl.WmItemRecptLineServiceImpl;
import org.example.erp.service.impl.WmItemRecptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class WmitemRecptController {
    @Autowired
    private WmItemRecptServiceImpl wmItemRecptService;
    @Autowired
    WarehouseService warehouseService;
    @Autowired
   WarehouseLocationService warehouseLocationService;
    @Autowired
    WarehouseAreaService warehouseAreaService;

    @Autowired
   private WmItemRecptLineServiceImpl wmItemRecptLineService;





    @GetMapping("/procurementWarehousing/find")
    public DataResult find(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam (required = false)String recptCode,
                           @RequestParam (required = false)String recptName,
                           @RequestParam (required = false)String supplierName,
                           @RequestParam (required = false)String poCode,
                           @RequestParam (required = false)String recptDate ){
        return DataResult.success(wmItemRecptService.find(currentPage,pageSize,recptCode,recptName,supplierName,poCode,recptDate));

    }


    @PostMapping ("/procurementWarehousing/insert")
    public DataResult insert(@RequestBody WmItemRecpt wmItemRecpt){
        QueryWrapper<Warehouse> q1=new QueryWrapper<>();
        q1.lambda().eq(Warehouse::getWarehouseId,wmItemRecpt.getWarehouseId());
        Warehouse one = warehouseService.getOne(q1);
        QueryWrapper<WarehouseArea> q2=new QueryWrapper<>();
        q2.lambda().eq(WarehouseArea::getWarehouseAreaId,wmItemRecpt.getWarehouseAreaId());
        WarehouseArea one1 = warehouseAreaService.getOne(q2);
        QueryWrapper<WarehouseLocation> q3=new QueryWrapper<>();
        q3.lambda().eq(WarehouseLocation::getWarehouseLocationCode,wmItemRecpt.getWarehouseLocationCode());
        WarehouseLocation one2 = warehouseLocationService.getOne(q3);
        wmItemRecpt.setWarehouseCode(one.getWarehouseCode());
        wmItemRecpt.setWarehouseAreaCode(one1.getWarehouseAreaCode());
        wmItemRecpt.setWarehouseLocationId(one2.getWarehouseLocationId());
        return DataResult.success(wmItemRecptService.save(wmItemRecpt));
    }

    @PutMapping("/procurementWarehousing/update")
    public DataResult update(@RequestBody WmItemRecpt wmItemRecpt){
        return DataResult.success(wmItemRecptService.updateById(wmItemRecpt));
    }


    @DeleteMapping("/procurementWarehousing/delete")
    public DataResult delete(@RequestBody List<Integer> logIds){
        return DataResult.success( wmItemRecptService.removeByIds(logIds));
    }


    @GetMapping("/line/find")
    public DataResult find(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam (required = false)String recptCode){
        return DataResult.success(wmItemRecptService.find(currentPage,pageSize,recptCode));
    }


    @PostMapping("/line/insert")
    public DataResult insert(@RequestBody WmItemRecptLine wmItemRecptLine){
        QueryWrapper<Warehouse> q1=new QueryWrapper<>();
        q1.lambda().eq(Warehouse::getWarehouseId,wmItemRecptLine.getWarehouseId());
        Warehouse one = warehouseService.getOne(q1);
        QueryWrapper<WarehouseArea> q2=new QueryWrapper<>();
        q2.lambda().eq(WarehouseArea::getWarehouseAreaId,wmItemRecptLine.getWarehouseAreaId());
        WarehouseArea one1 = warehouseAreaService.getOne(q2);
        QueryWrapper<WarehouseLocation> q3=new QueryWrapper<>();
        q3.lambda().eq(WarehouseLocation::getWarehouseLocationCode,wmItemRecptLine.getWarehouseLocationCode());
        WarehouseLocation one2 = warehouseLocationService.getOne(q3);
        wmItemRecptLine.setWarehouseCode(one.getWarehouseCode());
        wmItemRecptLine.setWarehouseAreaCode(one1.getWarehouseAreaCode());
        wmItemRecptLine.setWarehouseLocationId(one2.getWarehouseLocationId());
        return DataResult.success(wmItemRecptLineService.save(wmItemRecptLine));
    }

    @PutMapping("/line/update")
    public DataResult update(@RequestBody WmItemRecptLine wmItemRecptLine){
        return DataResult.success(wmItemRecptLineService.updateById(wmItemRecptLine));
    }

    @DeleteMapping("/line/delete")
    public DataResult delete2(@RequestBody List<Integer> logIds){
        return DataResult.success( wmItemRecptLineService.removeByIds(logIds));
    }


}
